package com.jsz.peini.controller.app;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.alibaba.fastjson.JSONObject;
import com.jsz.peini.bean.report.ReportInfo;
import com.jsz.peini.bean.report.ReportReason;
import com.jsz.peini.common.constants.Constants;
import com.jsz.peini.common.util.PeiniUtils;
import com.jsz.peini.mapper.comm.ImageFilterResultMapper;
import com.jsz.peini.mapper.report.ReportInfoMapper;
import com.jsz.peini.service.report.ReportInfoService;

@Controller
public class ReportInfoController {
	private static final Logger logger = LoggerFactory.getLogger(ReportInfoController.class);
	@Autowired
	private ReportInfoService questionService;
	@Resource
	private ImageFilterResultMapper imageFilterResultMapper;
	@Resource
	private ReportInfoMapper reportInfoMapper;
	
	
	/**
	 * 举报图片上传
	 * @param request
	 * @param file
	 * @param reportInfoId
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value="/setQuestion",method = RequestMethod.POST)
	@ResponseBody
	@Transactional
	public Object setQuestion(HttpServletRequest request,@RequestParam(value = "file",required=false) CommonsMultipartFile[] file,
			@RequestParam(value="userToken") String userToken,
			@RequestParam(value="content") String content,
			@RequestParam(value="reportType") Integer reportType,//1问题反馈；2用户举报；3举报商家；4任务举报；5相册图片举报;6广场图片举报；7商家图片举报; 8多人聚会举报 9官方聚会举报
			@RequestParam(value="reportId",required=false) String reportId,//相册图片时为相册图片id，广场时为广场图片id，商家图片时为商家id
			@RequestParam(value="reportCause") Integer reportCause){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		infoMap.put("resultCode", Constants.SUCCESS);
    	infoMap.put("resultDesc", "成功");
		// 判断文件是否为空  
    	try {  
    		
    		JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			ReportInfo reportInfo = new ReportInfo();
			reportInfo.setContent(content);
			reportInfo.setReportCause(reportCause);
			reportInfo.setReportId(reportId);
			reportInfo.setReportType(reportType);
			reportInfo.setUserId(userId);
			reportInfo.setReportTime(new Date());
			questionService.setQuestion(reportInfo,file);
    	} catch (Exception e) {  
    		logger.error(e.getMessage(),e);  
    		infoMap.put("resultCode", Constants.EXCEPTION);
    		infoMap.put("resultDesc", "网络请求失败");
    		throw e;
    	}  
	    return infoMap;
	}
	
	/**
	 * 举报图片上传
	 * @param request
	 * @param file
	 * @param reportInfoId
	 * @return
	 * @throws IOException
	 */
	@ResponseBody
	@RequestMapping(value="/setQuestionNull",method = RequestMethod.POST)
	public Object setQuestionNull(HttpServletRequest request,
			@RequestParam(value="userToken") String userToken,
			@RequestParam(value="content") String content,
			@RequestParam(value="reportType") Integer reportType,
			@RequestParam(value="reportId",required=false) String reportId,
			@RequestParam(value="reportCause") Integer reportCause
			){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		// 判断文件是否为空  
		try {
			
			JSONObject obj = PeiniUtils.validToken(userToken);
			if(obj.getInteger("resultCode") != 1){
				return obj;
			}
			String userId = obj.getString("userId");
			
			ReportInfo reportInfo = new ReportInfo();
			reportInfo.setContent(content);
			reportInfo.setReportCause(reportCause);
			reportInfo.setReportId(reportId);
			reportInfo.setReportType(reportType);
			reportInfo.setUserId(userId);
			
			int reportInfoNum=questionService.insertByType(reportInfo);
			if (reportInfoNum>0) {
				infoMap.put("resultCode", Constants.SUCCESS);
				infoMap.put("resultDesc", "成功");
			}	
		} catch (Exception e) {  
			logger.error(e.getMessage(),e);  
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "网络请求失败");
		}  
		return infoMap;
	}
	
	/**
	 * 获取举报原因列表
	 * @author zq
	 */
	@RequestMapping(value="/getReportReasons")
	@ResponseBody
	public Object getReportCodes(HttpServletRequest request,
			@RequestParam(value="type") Integer type //1问题反馈；2用户举报；3举报商家；4任务举报；5图片举报
			){
		LinkedHashMap<String, Object> infoMap = new LinkedHashMap<String, Object>();
		try{
			if(type == null || type < 1 || type > 5){
				infoMap.put("resultCode", Constants.EXCEPTION);
				infoMap.put("resultDesc", Constants.EXCEPTION_DESC);
				return infoMap;
			}
			List<ReportReason> list = PeiniUtils.reportTypeResns.get(type);
			if(list == null){
				list = new ArrayList<ReportReason>();
			}
			
			infoMap.put("reportReason", list);
			infoMap.put("resultCode", Constants.SUCCESS);
			infoMap.put("resultDesc", "成功");
		}catch (Exception e) {
			infoMap.put("resultCode", Constants.EXCEPTION);
			infoMap.put("resultDesc", "成功");
			logger.error(e.getMessage(),e);
		}
		return infoMap;
		
	}
	
}
